package timber.demo {

	/**
	Constructs a demo file header and background complete with link back to choppingblock.com

	@author Matthew Richmond <matthew@choppingblock.com>
	@version 0.1
	@history 2008-10-07

	@Copyright 2008 Matthew Richmond <matthew@choppingblock.com>
    * 
	* This file is part of Timber.
    * 
	* Timber is free software: you can redistribute it and/or modify
	* it under the terms of the GNU Lesser General Public License as published by
	* the Free Software Foundation, either version 3 of the License, or
	* (at your option) any later version.
    * 
	* Timber is distributed in the hope that it will be useful,
	* but WITHOUT ANY WARRANTY; without even the implied warranty of
	* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	* GNU Lesser General Public License for more details.
    * 
	* You should have received a copy of the GNU Lesser General Public License
	* along with Timber.  If not, see <http://www.gnu.org/licenses/>.

	@usage
	Activate with:
		<code>

		</code>

	*/

	import flash.display.*;
	import flash.filters.*;
	import flash.events.*;
	import flash.text.*;
	import flash.net.*;

	public class DemoHeader extends Sprite {

		public var BASE_COLOR:Number = 0x333333;
		private var _title:String;
		
		private var _creditField:TextField;
		private var _creditFormat:TextFormat;
		private var _creditFormatHover:TextFormat;

		// ------------------------------------
		// CONSTRUCTOR
		// ------------------------------------

		public function DemoHeader (title:String) {
			//trace("DemoHeader");

			_title = title;
			addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
		};

		// ------------------------------------
		// ACCESSORS
		// ------------------------------------

		// ------------------------------------
		// INIT METHODS
		// ------------------------------------

		private function init ():void{
			//trace("DemoHeader: init");
			
			create();
		};

		// ------------------------------------
		// CREATE / DESTROY
		// ------------------------------------
		
		private function create ():void{
			//trace("DemoHeader: create");
			
			// background
			var bg:Sprite = createBackground(stage.stageWidth-1, stage.stageHeight-1);
			bg.alpha = .5;
			addChild(bg);
			
			// outline stage
			var outline:Sprite = createBorder(stage.stageWidth-1, stage.stageHeight-1);
			addChild(outline);
			
			// header bar
			var bar:Sprite = createRectangle(stage.stageWidth-1, 20);
			addChild(bar);
			
			var barDS:DropShadowFilter = new DropShadowFilter();
			barDS.color = 0x000000;
			barDS.blurX = 10;
			barDS.blurY = 10;
			barDS.angle = 45;
			barDS.alpha = 0.5;
			barDS.distance = 0;
			
			var filterArray:Array = new Array(barDS);
			bar.filters = filterArray;
			
			// title
			var titleField:TextField = createTitle ();
			titleField.x = 10;
			titleField.y = 2;
			titleField.text = _title;
			addChild(titleField);
			
			// credit
			var creditSprite:Sprite = new Sprite();
			
			// credit format
			_creditFormat = new TextFormat()
			_creditFormat.font = "Arial";
			_creditFormat.color = 0x666666;
			_creditFormat.size = 10;
			
			_creditFormatHover = new TextFormat()
			_creditFormatHover.color = 0xFF6600;
			
			// credit text
			_creditField = createCredit (_creditFormat);
			_creditField.text = "a choppingblock demo";
			creditSprite.addChild(_creditField);
			
			// credit logo
			var chop:BlockLogo = new BlockLogo();
			creditSprite.addChild(chop);
			chop.filters = filterArray;
			
			// position
			_creditField.x = 0 - _creditField.textWidth - (chop.width * .5) - 5;
			chop.y = (chop.height * .5) - 1;
			creditSprite.x = stage.stageWidth - (chop.width * .5) - 5;
			creditSprite.y = 3;
			
			// action
			creditSprite.addEventListener(MouseEvent.MOUSE_OVER, creditMouseOverHandler, false, 0, true);
			creditSprite.addEventListener(MouseEvent.MOUSE_OUT, creditMouseOutHandler, false, 0, true);
			creditSprite.addEventListener(MouseEvent.CLICK, creditMouseClickHandler, false, 0, true);
			
			addChild(creditSprite);
		};
		
		private function createBorder (w:Number, h:Number):Sprite{
			//trace("DemoHeader: createBorder");
			
			var s:Sprite = new Sprite();
			s.graphics.lineStyle(1, BASE_COLOR);
			s.graphics.drawRect( 0, 0, w, h);
			s.graphics.endFill();
			
			return s;
		};
		
		private function createRectangle (w:Number, h:Number):Sprite{
			//trace("DemoHeader: createRectangle");
			
			var s:Sprite = new Sprite();
			s.graphics.beginFill(BASE_COLOR);
			s.graphics.drawRect( 0, 0, w, h);
			s.graphics.endFill();
			
			return s;
		};
		
		private function createBackground (w:Number, h:Number):Sprite{
			//trace("DemoHeader: createBackground");
			
			var bitmap:BitmapData = new Grain(152, 188);

			var s:Sprite = new Sprite();
			s.graphics.beginBitmapFill(bitmap);
			s.graphics.drawRect( 0, 0, w, h);
			s.graphics.endFill();
			
			return s;
		};
		
		private function createTitle ():TextField{
			//trace("DemoHeader: createTitle");
			
			var f:TextFormat = new TextFormat()
			f.font = "Arial Bold";
			f.color = 0xFFFFFF;
			f.size = 12;

			var field:TextField = new TextField()
			field.autoSize = TextFieldAutoSize.LEFT;
			field.selectable = false;
			field.antiAliasType = AntiAliasType.ADVANCED;
			field.defaultTextFormat = f;
			
			return field;
		};
		
		private function createCredit (format:TextFormat):TextField{
			//trace("DemoHeader: createCredit");
			
			var field:TextField = new TextField()
			field.autoSize = TextFieldAutoSize.LEFT;
			field.selectable = false;
			field.antiAliasType = AntiAliasType.ADVANCED;
			field.defaultTextFormat = format;
			
			return field;
		};
		
		// ------------------------------------
		// METHODS
		// ------------------------------------
		
		// ------------------------------------
		// EVENT METHODS
		// ------------------------------------
		
		private function addedToStageHandler (event:Event):void{
			//trace("DemoHeader: addedToStageHandler");
			init();
		};
		
		private function creditMouseOverHandler (event:MouseEvent):void{
			//trace("DemoHeader: creditMouseOverHandler");
			_creditField.setTextFormat(_creditFormatHover);
		};
		
		private function creditMouseOutHandler (event:MouseEvent):void{
			//trace("DemoHeader: creditMouseOutHandler");
			_creditField.setTextFormat(_creditFormat);
		};
		
		private function creditMouseClickHandler (event:MouseEvent):void{
			//trace("DemoHeader: creditMouseClickHandler");
			
			var request:URLRequest = new URLRequest("http://choppingblock.com");
			try {
				navigateToURL(request, "_self");
			} catch (error:Error) {
			    trace("DemoHeader: Unable to load requested document.");
			};
			
		};
		
	};
};